package com.commerceerp.system.service.Impl;

import com.commerceerp.system.entity.ForderInfo;
import com.commerceerp.system.mapper.ForderMapper;
import com.commerceerp.system.service.FactoryService;
import com.commerceerp.system.service.ForderService;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.StringUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ForderServiceImpl implements ForderService {

    @Autowired
    private ForderMapper forderMapper;

    /**
     * 根据不同状态获取财务单
     */
    @Override
    public List<Map> getAllForderInfoByStatus(ForderInfo forderInfo) {
        //订单
        if(StringUtils.equals("3",forderInfo.getForderStatus()) || StringUtils.equals("4",forderInfo.getForderStatus())){
            return forderMapper.getAllOrderInfoByStatus(forderInfo);
        }else{//采购单
            return forderMapper.getAllForderInfoByStatus(forderInfo);
        }
    }


    /**
     * 确定付款/确定收款
     */
    @Override
    public Integer updForderStatus(ForderInfo forderInfo) {

        Integer success = forderMapper.updForderStatus(forderInfo);

        return success;
    }


    /**
     * 新增财务单
     */
    @Override
    public Integer saveNewForderInfo(ForderInfo forderInfo) {

        Integer success = forderMapper.saveNewForderInfo(forderInfo);

        return success;
    }


    /**
     *新增财务明细记录
     */
    @Override
    public Integer saveNewForderRecord(String frForderId, String frPorderId, String frInOut, BigDecimal frMoney, String frRemark) {

        Integer success = forderMapper.saveNewForderRecord(frForderId,frPorderId,frInOut,frMoney, DateUtil.getYMDHMSDate(),frRemark);

        return success;
    }


    /**
     * 获取所有财务明细
     */
    @Override
    public List<Map> getAllForderRecord(String frForderId, String frPorderId, BigDecimal frMoney, String frInOut, String beginTime, String endTime) {

        List<Map> list = forderMapper.getAllForderRecord(frForderId,frPorderId,frMoney,frInOut,beginTime,endTime);

        return list;
    }


    /**
     * 驳回采购单
     * @return
     */
    @Override
    public Integer updForderToReject(ForderInfo forderInfo) {

        Integer success = forderMapper.updForderToReject(forderInfo);

        return success;
    }


    /**
     * 财务页面的数量
     * @return
     */
    @Override
    public List<Map> getCountByForderStatus() {
        List<Map> list = Lists.newArrayList();
        //待付款：是采购的时候生成采购单之后会生成一笔待付款的记录
        Map map = new HashMap();
        map.put("forderStatus",1);
        map.put("number",forderMapper.getCountByPorderStatus(3,3));
        list.add(map);
        //已付款：只是已经支付过的的采购单记录
        Map map1 = new HashMap();
        map1.put("forderStatus",2);
        map1.put("number",forderMapper.getCountByPorderStatus(4,5));
        list.add(map1);
        //待收款：是获取订单的数据，订单的商品发货了但是没有收到用户支付的金额记录
        Map map2 = new HashMap();
        map2.put("forderStatus",3);
        map2.put("number",forderMapper.getCountByOrderStatus(4,4));
        list.add(map2);
        //已收款：是指用户支付了订单数据，然后由人工操作待收款上面记录的“确定收款”按钮，则待收款的记录会到此处
        Map map3 = new HashMap();
        map3.put("forderStatus",4);
        map3.put("number",forderMapper.getCountByOrderStatus(7,7));
        list.add(map3);
        //申请驳回，是针对待付款的，对这笔采购单的待付款进行驳回
        Map map4 = new HashMap();
        map4.put("forderStatus",5);
        map4.put("number",forderMapper.getCountByPorderStatus(6,6));
        list.add(map4);
        return list;
    }


    /**
     * 获取财务总收入，支出
     */
    @Override
    public Map<String, Object> getAllInOutMoney(String frForderId, String frPorderId, BigDecimal frMoney, String frInOut, String beginTime, String endTime) {

        List<Map> list = forderMapper.getAllInOutMoney(frForderId,frPorderId,frMoney,frInOut,beginTime,endTime);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("in",0.0);
        map.put("out",0.0);

        for(Map<String,Object> tmp : list){

            if("1".equals(tmp.get("frInOut").toString())){

                map.put("out",Double.valueOf(tmp.get("money")== null ? "0.0" : map.get("money").toString()));

            }else if("2".equals(tmp.get("frInOut").toString())){

                map.put("in",Double.valueOf(tmp.get("money")== null ? "0.0" : tmp.get("money").toString()));

            }

        }

        Double profit = Double.valueOf(map.get("in")== null ? "0.0" : map.get("in").toString())-Double.valueOf(map.get("out") == null ? "0.0" : map.get("out").toString());
        map.put("profit",profit);

        return map;


    }
}
